package com.onstar.cn.common.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.telephony.TelephonyManager;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.log.LogManager;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.UUID;

/**
 *
 *
 */
public class DeviceUtil {
    private Context mContext;
//    private TelephonyManager mTelephonyManager;
//    private LogManager logger;

    public DeviceUtil(Context context) {
        mContext = context;
//        mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
//        logger = LogManager.getInstance(context);
    }

    public String getIMSI() {
//        return !CommonUtil.isNull(mTelephonyManager.getSubscriberId()) ? mTelephonyManager.getSubscriberId() : "1";/* "460023028363189" */
         return "123";
    }

    public String getAppVersion() {
        ApplicationInfo appInfo = mContext.getApplicationInfo();
        PackageManager pm = mContext.getPackageManager();
        try {
            return pm.getPackageInfo(appInfo.packageName, 0).versionName;
        } catch (NameNotFoundException e) {
//            logger.d(e.getMessage());
        }
        return null;
    }

    public int getAppVersionCode() {
        ApplicationInfo appInfo = mContext.getApplicationInfo();
        PackageManager pm = mContext.getPackageManager();
        try {
            return pm.getPackageInfo(appInfo.packageName, 0).versionCode;
        } catch (NameNotFoundException e) {
//            logger.d(e.getMessage());
        }
        return 0;
    }

    public String getDeviceID() {
        if (CommonUtil.isNull(Constants.DEVICE_ID)) {
            return getUniquePsuedoId();
        } else {
            return Constants.DEVICE_ID;
        }
//        return mTelephonyManager.getDeviceId()/* "358326034114493" */;
    }

    public static String getOSVersion() {
        return Build.VERSION.RELEASE;
    }

    /**
     * Get current mobile
     *
     * @return
     */
//    public String getPhoneNumber() {
//        return mTelephonyManager.getLine1Number();
//    }

    public static String getLanguage() {
        return Locale.getDefault().getLanguage();
    }

    public static String getCountry() {
        return Locale.getDefault().getCountry();
    }

    public static String getDeviceType() {
        return android.os.Build.MODEL;
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static boolean isChineseLanguage() {
        return Locale.CHINA.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
                || Locale.CHINESE.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
                || Locale.PRC.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
                || Locale.TAIWAN.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
                || Locale.TRADITIONAL_CHINESE.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage());
    }

    /***
     * 获取网关IP地址,据安全要求，不建议使用此方法
     *
     * @return
     */
    @Deprecated
    public static String getHostIp() {
//		try {
//			for (Enumeration<NetworkInterface> en = NetworkInterface
//					.getNetworkInterfaces(); en.hasMoreElements();) {
//				NetworkInterface intf = en.nextElement();
//				for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr
//						.hasMoreElements();) {
//					InetAddress inetAddress = ipAddr.nextElement();
//					if (!inetAddress.isLoopbackAddress()) {
//						return inetAddress.getHostaaaaaaaAddress();
//					}
//				}
//			}
//		} catch (SocketException ex) {
//		} catch (Exception e) {
//		}
        return null;
    }


    public String getLocalIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException ex) {
        }
        return null;
    }


    public String getUniquePsuedoId() {
        String deviceShort = "35" + Build.BOARD.length() % 10 +
                Build.BRAND.length() % 10 +
                Build.CPU_ABI.length() % 10 +
                Build.DEVICE.length() % 10 +
                Build.DISPLAY.length() % 10 +
                Build.HOST.length() % 10 +
                Build.ID.length() % 10 +
                Build.MANUFACTURER.length() % 10 +
                Build.MODEL.length() % 10 +
                Build.PRODUCT.length() % 10 +
                Build.TAGS.length() % 10 +
                Build.TYPE.length() % 10 +
                Build.USER.length() % 10; //13 digits
        String serial = Build.SERIAL;
        String uuid = new UUID(deviceShort.hashCode(), serial.hashCode()).toString();
        Constants.DEVICE_ID = uuid;
        return uuid;
    }
}
